<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * 公用模块
 * Class CommonController
 */
class CommonController extends CI_Controller
{
	/**
	 * @throws MyException
	 */
	protected function _auth()
	{
		if (!$this->auth->isLogin())
			throw new MyException('', MyException::AUTH);
	}

	/**
	 * 修改用户个人信息
	 */
	function modMe()
	{
		$input = $this->input->put(['nickName', 'pwd', 'oldpwd']);
		if (!$input) throw new MyException('', MyException::INPUT_MISS);
		if ($input['oldpwd'] != '') {
			if (!$this->account->updatePwd($input['oldpwd'],$input['pwd'])) throw new MyException('', MyException::DATABASE);
		}
		if ($this->db->where('id', UID)->update('user', ['nickName' => $input['nickName']])) ;
		else throw new MyException('', MyException::DATABASE);
	}

	/**
	 * 获取用户个人信息
	 */
	function me()
	{
		$info = $this->db->find('user', UID, 'id', 'openid');
		restful(200, $info);
	}


	/**
	 * 绑定统一认证
	 */
	function addBind()
	{
		$code = $this->input->post('code');
		if (!$code) throw new MyException('', MyException::INPUT_MISS);
		$this->load->library('oauth');
		$info = $this->oauth->userInfo($code);
		if (isset($info['openid'])) {
			$update = ['openid' => $info['openid'], 'oname' => $info['nickName']];
			$succ = $this->db->update('user', $update, ['id' => UID]);
			if ($succ) {
				restful(201, $update);
			} else throw new MyException('', MyException::DATABASE);
		} else {
			throw new MyException('认证失败！请重试', MyException::THIRD);
		}
	}


	/**
	 * 菜单列表
	 */
	function menu()
	{
		$admin=false;
		$roledata=$this->db->select('rid')
				->where('uid='.UID)
				->get('userRole')->result_array();
		$role_arr=[];

		foreach($roledata as $val){
			if($val['rid']==1){
				$admin=1;
				break;
			}else{
				$role_arr[]=intval($val['rid']);
			}
		}

		if ($admin == 1) {
			if (ENVIRONMENT !== 'development')
				$this->db->group_start()->where('menu.id <', 8)->or_where('menu.id >', 9)->group_end();
			$menudata = $this->db->select('id,name,`index` AS path,parentId,sort,img,icon')
				->order_by('sort','asc')
				->where('disabled',0)
				->get('menu')->result_array();
			restful(200, $menudata);
		}else{
			if (!$role_arr) restful(200,[]);
			$sub=$this->db->select('module.mid')->from('module')
			->where_in('roleModule.rid',$role_arr)
			->join('roleModule', 'roleModule.mid = module.id')
			->get_compiled_select();
			if(ENVIRONMENT !== 'development')
				$this->db->where('menu.id <',8)->where('menu.id >',9);
			$menudata = $this->db->select('menu.id,menu.name,menu.`index` AS path,menu.parentId,img,icon')
			->where("id in ($sub)",null,false)
			->order_by('menu.sort','asc')
			->where('disabled',0)
			->get('menu')->result_array();
			//处理父子节点
			$has=array_column($menudata,'id');
			$addition=[];
			foreach ($menudata as $value) {
				while ($value['parentId']>0 && !in_array($value['parentId'],$has)) {
					$value=$this->db->find('menu',$value['parentId'],'id','menu.id,menu.name,menu.`index` AS path,menu.parentId,img,icon');
					$addition[]=$value;
					$has[]=$value['id'];
				}
			}
			restful(200, array_merge($menudata,$addition));
		}
	}

	/**
	 * 权限列表
	 */
	function rights($path = '')
	{
		if ($path == '') {
			$input = $this->input->get('index');
			if (!$input) throw new MyException('', MyException::INPUT_MISS);
			$res = [];
			if (is_array($input)) {
				foreach ($input as $value) {
					$res[$value] = $this->rights($value);
				}
			} else $res[$input] = $this->rights($input);
			restful(200, $res);
		} else {
			if (in_array(1, $this->auth->groups)) {
				return $this->db->select('`index`')
					->like('`index`', $path, 'after')
					->get('rights')->result_array();
			} else return $this->db->where('id in (SELECT moduleRights.rid FROM roleModule join moduleRights on moduleRights.mid=roleModule.mid WHERE roleModule.rid in ' .
				'(SELECT rid FROM userRole WHERE uid=' . UID . ') )')
				->like('`index`', $path, 'after')
				->select('`index`')
				->get('rights')->result_array();
		}
	}


	/**
	 * 退出登录
	 */
	function logout()
	{
		$this->db->where('id', UID)->update('user', ['token' => uniqid()]);
		restful(200);
	}
}
